<template>
  <div :class="className" :style="{height:height,width:width}"/>
</template>

<script>
import echarts from 'echarts'
import 'echarts/map/js/china'

export default {
  name: 'ChinaMap',
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '300px'
    },
    chartData: {
      type: Object,
      required: true
    }
  },
  watch: {
    chartData: {
      deep: true,
      handler(val) {
        this.setOptions(val)
      }
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart()
    })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, 'macarons')
      // this.setOptions(this.chartData)
    },

    setOptions({ data, count, name, color, position, dataZoom } = {}) {
      this.chart.setOption({
        tooltip: {
          trigger: 'item'
        },

        geo: {
          map: 'china',
          scaleLimit: {
            min: 1,
            max: 2
          },
          zoom: 1,
          label: {
            normal: {
              show: true,
              fontSize: '14',
              color: 'rgb(217,114,82)'
            }
          },
          itemStyle: {
            normal: {
              //shadowBlur: 50,
              //shadowColor: 'rgba(0, 0, 0, 0.2)',
              borderColor: 'rgba(0, 0, 0, 0.2)'
            },
            emphasis: {
              areaColor: '#f2d5ad',
              shadowOffsetX: 0,
              shadowOffsetY: 0,
              borderWidth: 0
            }
          }
        },

        visualMap: {
          min: 0, // 最小值
          max: data[0].value || 2000, // 最大值
          text: ['高', '低'],
          realtime: false,
          calculable: false,
          inRange: {
            color: ['#e6f7ff', '#1890FF', '#0050b3'] // 渐变颜色
          }
        },
        legend: {
          data: [name]
        },
        series: [{
          name: '网关数量',
          map: 'china',
          type: 'map',
          // roam: true,
          data: data,
          geoIndex: 0
        }]
      })
    }
  }
}
</script>

<style scoped>

</style>
